<template>
  <div class="app-container">
    <el-table
      :data="list"
      border
      fit
      highlight-current-row>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" width="160" />
      <el-table-column prop="email" label="邮箱"/>
      <el-table-column label="状态" width="160">
        <template slot-scope="scope">
          <el-tag v-show="scope.row.status === 0" type="success">正常</el-tag>
          <el-tag v-show="scope.row.status === 1" type="danger">被封禁</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="清洗次数">
        <template slot-scope="scope">
          {{ scope.row.cleannum === -1?'无限制': scope.row.cleannum }}
        </template>
      </el-table-column>
      <el-table-column label="分析次数">
        <template slot-scope="scope">
          {{ scope.row.analyzenum === -1?'无限制': scope.row.cleannum }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status===1" type="primary" size="mini" icon="el-icon-edit" @click="enable(scope.row.uid)">解封</el-button>
          <el-button v-if="scope.row.status===0" type="danger" size="mini" icon="el-icon-edit" @click="disable(scope.row.uid)">禁用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      :page-sizes="[5,10, 20, 50]"
      style="padding: 30px 0; text-align: center;"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>
<script>
import Admin from '@/api/admin.js'

export default{
  data() {
    
    return {
      page: 1,
      limit: 5,
      total: 0,
      list: null,
      adminInfo: {
        name: '',
        password: ''
      }
    }
  },

  created() {
    this.getList()
  },

  methods: {
    // 获得列表
    getList() {
      Admin.getUserList(this.page, this.limit)
        .then(response => {
          this.list = response.data.item
          this.pageTotal = response.data.item.length
        })
        .catch(error => {
        })
    },
    // 分页
    handleCurrentChange(val) {
      this.page = val
      this.getList()
    },
    handleSizeChange(val) {
      this.limit = val
      this.page = 1
      this.getList()
    },
    // 封禁帐号
    disable(id) {
      this.$confirm('此操作将导致该账号无法使用, 是否继续?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Admin.disableUser(id)
          .then(response => {
            this.getList()
          })
          .catch(() => {})
      }).catch(() => {})
    },
    // 解封账号
    enable(id) {
      Admin.abledUser(id)
        .then(response => {
          this.getList()
        })
        .catch(() => {})
    },
    // 删除账号
    removeDataById(id) {
      this.$confirm('此操作将管理员账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Admin.deleteone(id)
          .then(Request => {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.getList()
          })
      }).catch(() => { })
    }
  }
}
</script>
